#include"../include/lib_headers.h"
char* make_dir(char *pt, char *dt) {

	int i = 0, j = 0, k = 0, match = 0, t = 0, y = 1, counter = 0;	
	char *s, input[SIZE] = "", *split_char, *p[SIZE], copy[SIZE] = "";
	char parent[SIZE] = "", directory[SIZE] = "";
	node *temp = NULL , *prev = NULL ;
	
	/* VFS not mounted */
	if (h == NULL || l1 == NULL) {
		sprintf(output_message,"%s %s","makedir_FAILURE",ERR_VFS_MAKEDIR_05);	
		return output_message;
	}
	
	/* Insufficient Arguments */
	if(strlen(pt) == 0 || strlen(dt) == 0 || pt[0]!='/'){
		sprintf(output_message,"%s %s","makedir_FAILURE",ERR_VFS_MAKEDIR_00);	
		return output_message;
	}
	
	if(validate_name(dt) == FAILURE){
		sprintf(output_message,"%s %s","makedir_FAILURE",ERR_VFS_MAKEDIR_02);	
		return output_message;
	}
	
	/* File System is full */
	if (h->usedFds == h->maxFds) {
		sprintf(output_message,"%s %s","makedir_FAILURE",ERR_VFS_MAKEDIR_01);	
		return output_message;
	}

	pt = add_slash(pt);
	dt = add_slash(dt);
	strcpy(parent, pt);
	strcpy(directory, dt);
	strcat(parent, directory);

	/* Directory aready exists */
	if((l1!=0 && l1->next!=0)&&find_node(l1->next,parent)!=NULL){
		sprintf(output_message,"%s %s","makedir_FAILURE",ERR_VFS_MAKEDIR_03);	
		return output_message;
	}	

	split_char = "/";
	i = splits_directory(parent, p, split_char);
	
	/* CASE WHERE A NODE HAS BEEN CREATED UNDER THE NODE */
	if (l1->next == NULL) {
		j = 0;
		if (h->usedFds == h->maxFds) {
			sprintf(output_message,"%s %s","makedir_FAILURE",ERR_VFS_MAKEDIR_01);	
			return output_message;
		}
		
		node *n = (node*) malloc(sizeof(node));
		fd *f= (fd*) malloc(sizeof(fd));
		n->fd_pointer=f;
				
		strcpy(f->fileName, p[j]);
		strcpy(f->fullPath, "/");
		strcat(f->fullPath, p[j]);
		/* strcat(f->fullPath, "/"); */
		strcpy(copy, "/");
		strcat(copy, p[j]);
		/* strcat(copy, "/"); */
				
		l1->next = n;
		n->child = NULL;
		n->sibling = NULL;
		f->blockno= freelist_alloc();
		freelist[f->blockno]=1;
		h->usedFds++;
		strcpy(f->fileType,"dir");
		insert(f);
		htable_insert(hashtable,*f);
		j++;
		temp = n;
		

		while (j < i) {
			if (h->usedFds == h->maxFds) {
				sprintf(output_message,"%s %s","makedir_FAILURE",ERR_VFS_MAKEDIR_01);	
				return output_message;
			}
			
			fd *f= (fd*) malloc(sizeof(fd));
			node *n = (node*) malloc(sizeof(node));
			n->fd_pointer=f;			
			strcat(copy, "/");
			strcat(copy, p[j]);
			/* strcat(copy, "/"); */
			strcpy(f->fileName, p[j]);
			strcpy(f->fullPath, copy);
			temp->child = n;
			n->child = NULL;
			n->sibling = NULL;
			f->blockno= freelist_alloc();
			freelist[f->blockno]=1;			
			h->usedFds++;
			strcpy(f->fileType,"dir");
			insert(f);
			htable_insert(hashtable,*f);
			temp = n;
			j++;
		}
		sprintf(output_message,"%s","makedir_SUCCESS");	
		return output_message;
	}


	temp = l1->next;
	strcpy(copy, "");
	split_char = "/";
	i = splits_directory(parent, p, split_char);
	match = 0;

	if (l1->next != NULL) {
		j = 0;
		t = 0;
		temp = l1->next;
		while (j < i) {
			while (temp != NULL) {
				if (strcmp(temp->fd_pointer->fileName, p[j]) == 0) {
					match = 1;
					strcat(copy, "/");
					strcat(copy, p[j]);
					/* strcat(copy, "/"); */
					j++;
					prev = temp;
					temp = temp->child;
					if (temp == NULL) {
						t = 1;
					}
					if(j==i)
						break;
				}
				if (match == 0) {
					prev = temp;
					temp = temp->sibling;
				} else if (match == 1) {
					match = 0;
					continue;
				}
			}
			if (temp == NULL) {
				strcat(copy, "/");
				strcat(copy, p[j]);
				/* strcat(copy, "/"); */
				if (h->usedFds == h->maxFds) {
					sprintf(output_message,"%s %s","makedir_FAILURE",ERR_VFS_MAKEDIR_01);	
					return output_message;
				}		
				node *n = (node*) malloc(sizeof(node));
				fd *f= (fd*) malloc(sizeof(fd));
				n->fd_pointer=f;
				n->child = NULL;
				n->sibling = NULL;
				f->blockno= freelist_alloc();
				freelist[f->blockno]=1;
				h->usedFds++;
				strcpy(f->fileType,"dir");
				strcpy(f->fileName, p[j]);
				strcpy(f->fullPath, copy);
				insert(f);
				htable_insert(hashtable,*f);
				if (t == 1) { 
					prev->child = n;
				} else if (t == 0) { 
					prev->sibling = n;
					t = 1;
				}
				j++;
				temp = n;
			}
		}
		sprintf(output_message,"%s","makedir_SUCCESS");	
		return output_message;
	}
}
